import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'

export const constRoutes = [
	{
		path: '/login',
		component: () => import('@/views/Login'),
		meta: { hidden: true },
		name: 'login'
	},
	{
		path: '/signCompany',
		component: () => import('@/views/SignCompany'),
		meta: { hidden: true },
		name: 'signCompany'
	},
	{
		path: '/',
		component: () => import('@/views/Index'),
		redirect: '/dashboard',
		children: [{
			path: '/dashboard',
			component: () => import('@/views/Dashboard'),
			name: 'Dashboard',
			meta: { title: '控制台', icon: 'dashboard', key: -1 },

		},
		{
			path: '/notAllow',
			component: () => import('@/views/notAllowed'),
			name: 'notAllow',
			meta: { title: '没有权限', icon: "dashboard", key: 2, hidden: 1 },

		}
		]
	},

	{
		path: '/404',
		component: () => import('@/views/404'),
		meta: { hidden: true },
		name: '404'
	}
];

const router = createRouter({
	history: createWebHashHistory(),
	// history: createWebHistory(),
	routes: constRoutes,

	scrollBehavior(to, from, savedPosition) {
		//滚动到顶部
		return {
			top: 0
		}
	}
});

import VueCookies from 'vue-cookies';

import { storeToRefs } from "pinia";
import { userStore } from "./store";
import { Modal } from 'ant-design-vue';

router.beforeEach(async (to, from, next) => {
	const token = VueCookies.get('token');

	if (!token) {
		if (to.path === '/login' || to.path === '/signCompany') {
			next();
		} else {
			next(`/login?redirect=${to.path}`)
		}
	} else {
		if (to.path === '/login') {
			next({ path: '/' });
		} else {
			//获取用户信息
			const store = userStore();
			//直接解构store得到数据不是响应式的，需要通过storeToRefs()才能转换为响应式数据对象
			const { info: userInfo, routes } = storeToRefs(store);

			//修改store值
			//方法一：直接赋值
			/*
			store.info.id = 1;
			*/

			//方法二：$patch
			/*
			store.$patch({
				//如果info还有其他属性，这种方法会丢失其他属性
				//info: { id: 1 },
				//可以先解构，再修改
				info: {
					...store.info,
					id: 1
				}
			});
			store.$patch(state => {
				state.info.id = 1;
			});
			*/

			//方法三：
			if (!userInfo.value.id) await store.setInfo();

			//强制重置密码
			if (userInfo.value.reset_password && to.path !== '/dashboard') {
				Modal.error({
					title: '请重置您的初始密码',
					content: '操作方法：点击右上角用户名，然后点击“修改密码”，输入新的密码后，点击“确定”完成操作。'
				});
				next({ path: '/dashboard' });
			} else if (userInfo.value.is_supplier && !userInfo.value.certificate_file_path && to.path !== '/dashboard') {
				Modal.error({
					title: '请配置账号证件信息',
					content: '操作方法：点击右上角用户名，然后点击“证件管理”，补充证件信息。'
				});
				return next({ path: '/dashboard' });
			} else if (userInfo.value.status === -1 && to.path !== '/dashboard') {
				Modal.error({
					title: '提示',
					content: '账号正在审核中。'
				});
				return next({ path: '/dashboard' });
			} else if (userInfo.value.status === 0 && to.path !== '/dashboard') {
				Modal.error({
					title: '提示',
					content: '账号已被禁用。'
				});
				return next({ path: '/dashboard' });
			}

			//判断是否注册过路由
			if (routes.value.length > 0) {
				next();
			} else {
				//动态注册路由
				await store.setRoutes();

				//注册路由
				routes.value.forEach((route) => {
					router.addRoute(route);
				});

				//[Vue Router warn]: No match found for location with path
				/*
				在路由鉴权beforeEach时，动态路由并未加载，因此刷新时会提示路由不能存在，replace: true再跳转一次。
				*/
				next({ ...to, replace: true });
			}
		}
	}
});

router.afterEach(() => {
	//window.scrollTo(0, 0);
})

export default router;